package com.chenyun.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyun.common.context.SpringContext;
import com.chenyun.common.vo.AreaVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 区域操作类
 *
 * @author: Lonni
 * @date: 2022/7/6 0006 16:15
 */
public class AreaUtil {
    private final static Logger log = LoggerFactory.getLogger(AreaUtil.class);
    private final  static  int exprise=30; //天
    /**
     * 所有省
     */
    private final static String province_all_key = "province:all";


    /**
     * 所有市
     */
    private final static String city_key = "city";

    /**
     * 所有区
     */
    private final static String region_key = "region";

    private final static String tree_key = "areaTree";


    private final static RedisTemplate redisTemplate = SpringContext.getBean(RedisTemplate.class);

   static void expire(String key){
        redisTemplate.expire(key,exprise, TimeUnit.DAYS);
    }

    /**
     * 将所有省设置到缓存中
     *
     * @param provinces
     */
    public static void setProvinceAllCache(List<AreaVo.Province> provinces) {
        try {
            redisTemplate.opsForList().rightPushAll(province_all_key, provinces);
            expire(province_all_key);
        } catch (Exception ex) {
            log.debug("设置所有省缓存失败:{}", ex.getMessage());
        }
    }

    /**
     * 设置省下的市
     *
     * @param provinceCode
     * @param cityList
     */
    public static void setCity(String provinceCode, List<AreaVo.City> cityList) {
        try {
            redisTemplate.opsForHash().put(city_key, provinceCode, cityList);
            expire(city_key);
        } catch (Exception ex) {
            log.debug("设置市缓存失败:{}", ex.getMessage());
        }
    }

    /**
     * 设置市下的区
     *
     * @param regionList
     */
    public static void setRegionAllCache(String cityCode, List<AreaVo.Region> regionList) {
        try {
            redisTemplate.opsForList().rightPushAll(region_key, cityCode, regionList);
            expire(region_key);
        } catch (Exception ex) {
            log.debug("设置所有区缓存失败:{}", ex.getMessage());
        }
    }


    /**
     * 获取所有省份
     *
     * @return
     */
    public static List<AreaVo.Province> getProvince() {
        //索引从0开始
        List provinces = redisTemplate.opsForList().range(province_all_key, 0, 30);
        return provinces;
    }

    /**
     * 获取省下的市
     *
     * @param provinceCode
     * @return
     */
    public static List<AreaVo.City> getCity(String provinceCode) {
        List citys = (List) redisTemplate.opsForHash().get(city_key, provinceCode);
        return citys;
    }

    public static List<AreaVo.Region> getRegion(String cityCode) {
        List regions = (List) redisTemplate.opsForHash().get(region_key, cityCode);
        return regions;
    }


    /**
     * 获取省市区 树形结构
     *
     * @return
     */
    public static List<AreaVo.AreaTree> getProvinceCityRegionWithTree() {
        List range = redisTemplate.opsForList().range(tree_key, 0, 30);
        if (CollectionUtil.isNotEmpty(range)) {
            return range;
        }
        //不存在
        List<AreaVo.Province> provinces = getProvince();
        List<AreaVo.AreaTree> treeList = new ArrayList<>();
        for (AreaVo.Province province1 : provinces) {
            AreaVo.AreaTree parent = new AreaVo.AreaTree();
            parent.setLabel(province1.getName());
            parent.setValue(province1.getCode());

            List<AreaVo.City> citys = getCity(province1.getCode());
            List<AreaVo.AreaTree> cityTrees = new ArrayList<>();
            for (AreaVo.City city : citys) {
                AreaVo.AreaTree cityTree = new AreaVo.AreaTree();
                cityTree.setLabel(city.getName());
                cityTree.setValue(city.getCode());
                cityTrees.add(cityTree);
                //查询区
                List<AreaVo.Region> regions = getRegion(city.getCode());
                List<AreaVo.AreaTree> regionTrees = new ArrayList<>();
                for (AreaVo.Region region : regions) {
                    AreaVo.AreaTree regionTree = new AreaVo.AreaTree();
                    regionTree.setLabel(region.getName());
                    regionTree.setValue(region.getCode());
                    regionTrees.add(regionTree);
                }

                cityTree.setChildren(regionTrees);
            }
            parent.setChildren(cityTrees);
            treeList.add(parent);

        }
        if (CollectionUtil.isNotEmpty(treeList)) {
            //加入到缓存
            redisTemplate.opsForList().rightPushAll(tree_key, treeList);
            expire(tree_key);
        }
        return treeList;
    }


}
